//
//  GYStatusPictureView.swift
//  Weibo
//
//  Created by vane on 16/8/14.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
private let GYStatusPictureViewCellId = "GYStatusPictureViewCellId"

private let margin: CGFloat = 5
private let cellWH: CGFloat = 80

class GYStatusPictureView: UICollectionView {
    
    var pictureInfos:[GYPictureInfo]?{
        
        didSet{
            
//            countLabel.text = "\(pictureInfos?.count ?? 0)"
            
            let size = getPictureViewSize(pictureInfos?.count ?? 0)
           
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            
            reloadData()

            
        }
        
        
    }
    func getPictureViewSize(count: Int) -> CGSize{
        
        let col = count == 4 ? 2:count > 3 ? 3 : count
        let row = count == 4 ? 2:(count - 1)/3 + 1
        
        let w = CGFloat(col) * cellWH + CGFloat(col - 1)*margin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1)*margin
        
        return CGSize(width: w, height: h)
    }
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout:
            UICollectionViewFlowLayout())
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        backgroundColor = UIColor.clearColor()
        
        addSubview(countLabel)
        
        countLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        dataSource = self
        
        registerClass(GYStatusPictureViewCell.self, forCellWithReuseIdentifier: GYStatusPictureViewCellId)
        
        let layout = self.collectionViewLayout as!UICollectionViewFlowLayout
        
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        
        layout.minimumInteritemSpacing = margin
        
        layout.minimumLineSpacing = margin

    }
    
       private lazy var countLabel: UILabel = UILabel(textColor: UIColor.redColor(), textSize: 35)
}
extension GYStatusPictureView:UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pictureInfos?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(GYStatusPictureViewCellId, forIndexPath: indexPath) as! GYStatusPictureViewCell
        
        cell.picUrl = pictureInfos![indexPath.item]
        
        return  cell
    }
}
class GYStatusPictureViewCell: UICollectionViewCell {
    
    var picUrl: GYPictureInfo?{
        didSet{
            bgImageView.gy_setImageWithString(picUrl?.thumbnail_pic, placeholderImgName: "weibo_placeholder")
            
            if let pic = picUrl?.thumbnail_pic where pic.hasSuffix(".gif") {

                gifImageView.hidden = false
            }else {

                gifImageView.hidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
//        backgroundColor = RandomColor()
        addSubview(bgImageView)
        addSubview(gifImageView)
        
        bgImageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.trailing.equalTo(bgImageView)
        }
        
    }
    private lazy var bgImageView: UIImageView = {
        let img = UIImageView(imgName: "weibo_placeholder")
        img.contentMode = .ScaleAspectFill
        img.clipsToBounds = true
        return img
    }()
     private lazy var gifImageView: UIImageView = UIImageView(imgName: "timeline_image_gif")

}